import type { App } from "vue";
import { InjectionKey } from "vue";
import { createStore, Store, useStore as baseUseStore } from "vuex";

import { RootStateTypes } from "./interface/index";
import routes from "./modules/router";
import settings from "./modules/settings";

// InjectionKey 将store安装到Vue应用程序时提供类型，将类型传递InjectionKey给useStore方法
// 定义注入类型
const key: InjectionKey<Store<RootStateTypes>> = Symbol();

const store = createStore<RootStateTypes>({
	modules: {
		settings, // 登录后数据存储、系统参数
		routes // 路由数据、清洗登
	}
});

// 将类型注入useStore
// 以后项目中引用的均为自定义的这个，而不是vuex默认导出的useStore
export function useStore() {
	return baseUseStore(key);
}

export function setupStore(app: App<Element>) {
	app.use(store, key);
}

export default store;
